package v1

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"maya-service/bll"
	"maya-service/model"
	"maya-service/model/po"
	"maya-service/server/web/middleware"
	"maya-service/utils"
)

var Product = &product{}

func init() {
	RegisterRouter(Product)
}

type product struct{}

// Init
func (a *product) Init(r *gin.RouterGroup) {
	g := r.Group("/product", middleware.Auth())
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
		g.POST("/serviceList", a.serviceList)
	}
}

// serviceList 获取服务列表
func (a *product) serviceList(c *gin.Context) {
	var (
		in  = &model.ProductListRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err := bll.Product.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	} else {
		ret := po.ServiceListRsp{}
		listFlagMap := map[int]*po.ServiceListItem{}
		for _, v := range out.List {
			if _, ok := listFlagMap[v.ProductType]; !ok {
				listFlagMap[v.ProductType] = &po.ServiceListItem{
					Level: v.ProductType,
				}
				ret.List = append(ret.List, listFlagMap[v.ProductType])
			}

			pDetail := &po.ProductDetail{}
			_ = json.Unmarshal([]byte(v.ExtraContext), pDetail)
			pDetail.Id = v.Id
			listFlagMap[v.ProductType].ServiceList = append(listFlagMap[v.ProductType].ServiceList, pDetail)
		}
		utils.ResponseOk(c, ret)
		return
	}
}

// create
func (a *product) create(c *gin.Context) {
	var (
		in  = &model.ProductCreateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Product.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// update
func (a *product) update(c *gin.Context) {
	var (
		in  = &model.ProductUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Product.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *product) list(c *gin.Context) {
	var (
		in  = &model.ProductListRequest{}
		out = &model.ProductListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Product.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// list
func (a *product) find(c *gin.Context) {
	var (
		in  = &model.ProductInfoRequest{}
		out = &model.ProductInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.Product.Find(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// delete
func (a *product) delete(c *gin.Context) {
	var (
		in  = &model.ProductDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.Product.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
